package com.wlt.controller;

import com.alibaba.fastjson.JSON;
import com.wlt.pojo.Orders;
import com.wlt.pojo.Payment;
import com.wlt.result.BaseResult;
import com.wlt.service.OrdersService;
import com.wlt.service.ZfbPayService;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付
 */
@RestController
@RequestMapping("/user/payment")
public class PaymentController {
    @DubboReference
    private ZfbPayService zfbPayService;
    @DubboReference
    private OrdersService ordersService;
    
    /**
     * 生成二维码
     * @param orderId   订单id
     * @return          二维码
     */
    @PostMapping("/pcPay")
    public BaseResult<String> pcPay(String orderId) {
        Orders orders = ordersService.findById(orderId);
        String codeUrl = zfbPayService.pcPay(orders);
        
        return BaseResult.success(codeUrl);
    }
    
    /**
     * 支付成功的回调方法，用户扫码支付成功后支付宝调用的
     * 使用seata
     * @param request   请求对象
     * @return          无
     */
    @GlobalTransactional        // 分布式事务
    @PostMapping("/success/notify")
    public BaseResult successNotify(HttpServletRequest request) {
        // 验签
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, String[]> newMap = new HashMap<>();
        newMap.putAll(parameterMap);
        
        // 这个map不允许修改，所以要新建一个map创一个可修改的map
        Map<String, Object> paramMap = new HashMap<>();
        
        paramMap.put("requestParameterMap", newMap);
        zfbPayService.checkSign(paramMap);
        
        String trade_status = request.getParameter("trade_status"); // 订单状态
        String out_trade_no = request.getParameter("out_trade_no"); // 订单编号
        // 如果支付成功
        if (trade_status.equals("TRADE_SUCCESS")) {
            // 修改订单状态
            Orders orders = ordersService.findById(out_trade_no);
            // 状态改为已付款，支付宝支付，支付时间
            orders.setStatus(2);
            orders.setPaymentType(2);
            orders.setPaymentTime(new Date());
            ordersService.update(orders);
            // 添加支付消息记录
            Payment payment = new Payment();
            payment.setOrderId(out_trade_no);           // 订单编号
            payment.setTransactionId(out_trade_no);     // 交易号
            payment.setTradeType("支付宝支付");           // 支付方式
            payment.setTradeState(trade_status);        // 交易状态
            payment.setPayerTotal(orders.getPayment()); // 付款金额
            payment.setCreateTime(new Date());          // 付款时间
            payment.setContent(JSON.toJSONString(request.getParameterMap()));   // 支付详情
            
            zfbPayService.addPayment(payment);
        }
        
        return BaseResult.success();
    }
}
